<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="&amp;#x524D;&amp;#x8A00;&amp;#x76F8;&amp;#x4FE1;&amp;#x5927;&amp;#x90E8;&amp;#x5206;&amp;#x4EC1;&amp;#x5144;&amp;#x5728;&amp;#x4F7F;&amp;#x7528;&amp;#x7CFB;&amp;#x7EDF;Toast&amp;#x7684;&amp;#x65F6;&amp;#x5019;&amp;#xFF0C;&amp;#x90FD;&amp;#x611F;&amp;#x89C9;&amp;#x4E0D;&amp;#x592A;&amp;#x5C3D;&amp;#x">
<meta property="og:type" content="article">
<meta property="og:title" content="Android 自定义Toast，并且勘误Android工具类里面的ToastUtils">
<meta property="og:url" content="http://yoursite.com/2019/08/21/Android-自定义Toast，并且勘误Android工具类里面的ToastUtils/index.html">
<meta property="og:site_name" content="Silence潇湘夜雨">
<meta property="og:description" content="&amp;#x524D;&amp;#x8A00;&amp;#x76F8;&amp;#x4FE1;&amp;#x5927;&amp;#x90E8;&amp;#x5206;&amp;#x4EC1;&amp;#x5144;&amp;#x5728;&amp;#x4F7F;&amp;#x7528;&amp;#x7CFB;&amp;#x7EDF;Toast&amp;#x7684;&amp;#x65F6;&amp;#x5019;&amp;#xFF0C;&amp;#x90FD;&amp;#x611F;&amp;#x89C9;&amp;#x4E0D;&amp;#x592A;&amp;#x5C3D;&amp;#x">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1716569-ba28f705f413f968.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1716569-1abeee5865840f27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1716569-4c3c4f0a04a2ca9e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-07-19T05:31:32.369Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android 自定义Toast，并且勘误Android工具类里面的ToastUtils">
<meta name="twitter:description" content="&amp;#x524D;&amp;#x8A00;&amp;#x76F8;&amp;#x4FE1;&amp;#x5927;&amp;#x90E8;&amp;#x5206;&amp;#x4EC1;&amp;#x5144;&amp;#x5728;&amp;#x4F7F;&amp;#x7528;&amp;#x7CFB;&amp;#x7EDF;Toast&amp;#x7684;&amp;#x65F6;&amp;#x5019;&amp;#xFF0C;&amp;#x90FD;&amp;#x611F;&amp;#x89C9;&amp;#x4E0D;&amp;#x592A;&amp;#x5C3D;&amp;#x">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/1716569-ba28f705f413f968.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/08/21/Android-自定义Toast，并且勘误Android工具类里面的ToastUtils/">





  <title>Android 自定义Toast，并且勘误Android工具类里面的ToastUtils | Silence潇湘夜雨</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Silence潇湘夜雨</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Android开发一枚</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home //首页"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user //关于"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags  //标签"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th //分类"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive //归档"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar //日程表"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap //站点地图"></i> <br>
            
            站点地图
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/08/21/Android-自定义Toast，并且勘误Android工具类里面的ToastUtils/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Silence潇湘夜雨">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Silence潇湘夜雨">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android 自定义Toast，并且勘误Android工具类里面的ToastUtils</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-21T15:51:43+08:00">
                2019-08-21
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android设计模式/" itemprop="url" rel="index">
                    <span itemprop="name">Android设计模式</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.5k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="&#x524D;&#x8A00;"><a href="#&#x524D;&#x8A00;" class="headerlink" title="&#x524D;&#x8A00;"></a>&#x524D;&#x8A00;</h1><p><strong>&#x76F8;&#x4FE1;&#x5927;&#x90E8;&#x5206;&#x4EC1;&#x5144;&#x5728;&#x4F7F;&#x7528;&#x7CFB;&#x7EDF;Toast&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x90FD;&#x611F;&#x89C9;&#x4E0D;&#x592A;&#x5C3D;&#x5982;&#x4EBA;&#x610F;&#xFF0C;&#x56E0;&#x4E3A;&#x7CFB;&#x7EDF;Toast&#x663E;&#x793A;&#x7684;&#x4F4D;&#x7F6E;&#x6BD4;&#x8F83;&#x56FA;&#x5B9A;&#xFF0C;&#x5E76;&#x4E14;&#x5B57;&#x4F53;&#x989C;&#x8272;&#x7B49;&#x4F1A;&#x8DDF;&#x968F;&#x7CFB;&#x7EDF;&#x7248;&#x672C;&#x53D8;&#x5316;&#xFF0C;&#x90A3;&#x4E48;&#x80FD;&#x4E0D;&#x80FD;&#x81EA;&#x5DF1;&#x5199;&#x4E00;&#x4E2A;&#x5462;&#xFF0C;&#x7B54;&#x6848;&#x662F;&#x5F53;&#x7136;&#x7684;&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x8FD9;&#x662F;&#x672C;&#x6587;&#x7684;&#x4E00;&#x4E2A;&#x91CD;&#x70B9;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x91CD;&#x70B9;&#x662F;&#xFF0C;&#x8981;&#x5BF9;&#x4E0A;&#x4E00;&#x7BC7;<a href="http://www.jianshu.com/p/383411bd3e7e" target="_blank" rel="noopener">Android&#x5DE5;&#x5177;&#x7C7B;</a>&#x91CC;&#x9762;&#x7684;ToastUtils&#x8FDB;&#x884C;&#x52D8;&#x8BEF;&#xFF0C;&#x5927;&#x6982;&#x4F60;&#x73B0;&#x5728;&#x8FD8;&#x4E0D;&#x592A;&#x660E;&#x767D;&#xFF0C;&#x5BB9;&#x6211;&#x6162;&#x6162;&#x9053;&#x6765;&#xFF0C;&#x4F60;&#x4E14;&#x770B;&#x7740;&#x3002;</strong></p>
<hr>
<p><strong>&#x5148;&#x4E0A;&#x56FE;</strong></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1716569-ba28f705f413f968.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="
![&#x7CFB;&#x7EDF;Toast.png](http://upload-images.jianshu.io/upload_images/1716569-cf64bfc0bee87b5a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1716569-1abeee5865840f27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x4E0D;&#x5E26;&#x56FE;&#x7247;&#x7684;Toast&#xFF08;&#x4EE3;&#x7801;&#x91CC;&#x7F16;&#x8F91;&#x5E03;&#x5C40;&#xFF09;.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1716569-4c3c4f0a04a2ca9e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x5E26;&#x56FE;&#x7247;&#x7684;Toast.png"></p>
<hr>
<p>##&#x9996;&#x5148;<br><strong>&#x5148;&#x770B;toast&#x6E90;&#x7801;</strong></p>
<pre><code>   public void setText(CharSequence s) {    
    if (mNextView == null) {    
        throw new RuntimeException(&quot;This Toast was not created with Toast.makeText()&quot;);    
    }    
    TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.message);    
    if (tv == null) {    
        throw new RuntimeException(&quot;This Toast was not created with Toast.makeText()&quot;);    
    }    
    tv.setText(s);    
}  </code></pre><p><strong>&#x5982;&#x679C;&#x4E0D;&#x6307;&#x5B9A;view&#xFF0C;&#x5219;mNextView&#x7684;&#x503C;&#x4E3A;null&#x3002;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x3002;&#x53E6;&#x5916;&#x4F7F;&#x7528;&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x65F6;&#xFF0C;&#x5C31;&#x7B97;&#x4F60;&#x4E3A;Toast&#x8BBE;&#x7F6E;&#x597D;&#x4E86;View&#x4E5F;&#x4E0D;&#x80FD;&#x8FD9;&#x6837;&#x8C03;&#x7528;&#xFF1A;toast.setText(s);<br>&#x4F1A;&#x629B;&#x51FA;This Toast was not created with Toast.makeText()&#x5F02;&#x5E38;&#x3002;&#x89E3;&#x51B3;&#x7684;&#x529E;&#x6CD5;&#x662F;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;TextView&#xFF0C;&#x7136;&#x540E;&#x4E3A;TextView&#x8D4B;&#x503C;&#xFF0C;&#x518D;&#x52A0;&#x5230;XXLayout&#x4E2D;&#xFF0C;<br>&#x4F7F;&#x7528;setView(XXLayout)&#x628A;XXLayout&#x8BBE;&#x7F6E;&#x6210;Toast&#x7684;View&#x3002;</strong></p>
<p><strong>&#x672C;&#x4EBA;&#x9047;&#x5230;&#x7684;&#x9519;&#x8BEF;&#x6211;&#x4E5F;&#x4E00;&#x5E76;&#x8D34;&#x51FA;&#x6765;</strong><br>Caused by: java.lang.RuntimeException: This Toast was not created with Toast.makeText()<br>&#x200B;    at android.widget.Toast.setText(Toast.java:275)<br>&#x200B;    at cn.and.ToastActivity.onCreate(ToastActivity.java:20)<br>&#x200B;    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)<br>&#x200B;    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)</p>
<p><strong>&#x518D;&#x770B;&#x770B;<a href="http://www.jianshu.com/p/383411bd3e7e" target="_blank" rel="noopener">Android &#x5DE5;&#x5177;&#x7C7B;</a>&#x91CC;&#x9762;&#x7684;ToastUtils&#x5199;&#x6CD5;</strong></p>
<pre><code>   public static void showToast(Context context, String message) {
if (toast == null) {
    toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
} else {
    toast.setText(message);
}
toast.show();
}</code></pre><p><strong>&#x867D;&#x7136;&#x5E73;&#x65F6;&#x7528;&#x7740;&#x4E0D;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x4F46;&#x662F;&#x6709;&#x7684;&#x65F6;&#x5019;&#x8FD8;&#x662F;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x4E3A;&#x4E86;&#x9632;&#x60A3;&#x4E8E;&#x672A;&#x7136;&#xFF0C;&#x4ECA;&#x5929;&#x5C31;&#x91CD;&#x65B0;&#x653E;&#x51FA;&#x81EA;&#x5DF1;&#x67E5;&#x9605;&#x8D44;&#x6599;&#x540E;&#x4FEE;&#x6539;&#x7684;Toast&#x5DE5;&#x5177;&#x7C7B;</strong></p>
<p>##&#x5176;&#x6B21;&#xFF0C;&#x8BF7;&#x5404;&#x4F4D;&#x770B;&#x5B98;&#x770B;Toast&#x5DE5;&#x5177;&#x7C7B;</p>
<p><strong>&#x7531;&#x4E8E;&#x4EE3;&#x7801;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x6211;&#x4E5F;&#x5C31;&#x4E0D;&#x518D;&#x4E00;&#x4E00;&#x8D58;&#x8FF0;&#x4E86;&#xFF0C;&#x76F8;&#x4FE1;&#x5404;&#x4F4D;&#x770B;&#x5B98;&#x90FD;&#x53EF;&#x4EE5;&#x770B;&#x61C2;</strong></p>
<pre><code>  public class ToastUtils {
public static final int LENGTH_SHORT = 0;
public static final int LENGTH_LONG = 1;
private static View toastView;
private WindowManager mWindowManager;
private static int mDuration;
private final int WHAT = 100;
private static View oldView;
private static Toast toast;
private static CharSequence oldText;
private static CharSequence currentText;
private static ToastUtils instance = null;
private static TextView textView;
// &#x662F;&#x5426;&#x663E;&#x793A;
private static boolean isShow = true;
private static ImageView mImgIcon;
private static TextView mTxtMsg;
private static LinearLayout mLytShow;
   /**
 * &#x663E;&#x793A;&#x6D88;&#x606F;(&#x8FD9;&#x4E2A;&#x662F;&#x628A;&#x6587;&#x672C;&#x989C;&#x8272;&#x548C;&#x80CC;&#x666F;&#x8272;&#x6CA1;&#x6709;&#x63D0;&#x51FA;&#x6765;&#x7684;&#x5199;&#x6CD5;)
 * 
 * @param context
 *            &#x4E0A;&#x4E0B;&#x6587;
 * @param message
 *            &#x6D88;&#x606F;&#x5185;&#x5BB9;
 */
public static void showToast(Context context, String message) {
    if (isShow) {
        toast = new Toast(context);
        // &#x6D88;&#x606F;&#x5185;&#x5BB9;
        mTxtMsg = new TextView(context);
        LinearLayout.LayoutParams lParams1 = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mTxtMsg.setTextColor(Color.parseColor(&quot;#000000&quot;));
        mTxtMsg.setTextSize(DensityUtils.dp2px(context, 12));
        mTxtMsg.setPadding(10, 5, 10, 5);

        mTxtMsg.setLayoutParams(lParams1);

        mLytShow = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mLytShow.setOrientation(LinearLayout.HORIZONTAL);
        mLytShow.setLayoutParams(params);

        int roundRadius = 100; // 8dp &#x5706;&#x89D2;&#x534A;&#x5F84;
        int fillColor = Color.parseColor(&quot;#F89009&quot;);// &#x5185;&#x90E8;&#x586B;&#x5145;&#x989C;&#x8272;
        GradientDrawable gd = new GradientDrawable();// &#x521B;&#x5EFA;drawable
        gd.setCornerRadius(roundRadius);
        gd.setColor(fillColor);

        mLytShow.setBackgroundDrawable(gd);
        mLytShow.addView(mTxtMsg);

        if (message != null) {
            mTxtMsg.setText(message);
        }
        toast.setView(mLytShow);
        toast.setGravity(Gravity.BOTTOM, 0,
                DensityUtils.dp2px(context, 100));
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }
}
/**
 * &#x663E;&#x793A;&#x6D88;&#x606F;&#xFF08;&#x8FD9;&#x4E2A;&#x662F;&#x628A;&#x6587;&#x672C;&#x989C;&#x8272;&#x548C;toast&#x80CC;&#x666F;&#x989C;&#x8272;&#x63D0;&#x51FA;&#x6765;&#x7684;&#x5199;&#x6CD5;&#xFF09;
 * 
 * @param context
 *            &#x4E0A;&#x4E0B;&#x6587;
 * @param message
 *            &#x6D88;&#x606F;&#x5185;&#x5BB9;
 */
public static void showToast(Context context, String message,
        String textColor, String bgColor) {
    if (isShow) {
        toast = new Toast(context);
        // &#x6D88;&#x606F;&#x5185;&#x5BB9;
        mTxtMsg = new TextView(context);
        LinearLayout.LayoutParams lParams1 = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mTxtMsg.setTextColor(Color.parseColor(textColor));
        mTxtMsg.setTextSize(DensityUtils.dp2px(context, 12));
        mTxtMsg.setPadding(10, 5, 10, 5);

        mTxtMsg.setLayoutParams(lParams1);

        mLytShow = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mLytShow.setOrientation(LinearLayout.HORIZONTAL);
        mLytShow.setLayoutParams(params);

        int roundRadius = 100; // 8dp &#x5706;&#x89D2;&#x534A;&#x5F84;
        int fillColor = Color.parseColor(bgColor);// &#x5185;&#x90E8;&#x586B;&#x5145;&#x989C;&#x8272;
        GradientDrawable gd = new GradientDrawable();// &#x521B;&#x5EFA;drawable
        gd.setCornerRadius(roundRadius);
        gd.setColor(fillColor);

        mLytShow.setBackgroundDrawable(gd);
        mLytShow.addView(mTxtMsg);

        if (message != null) {
            mTxtMsg.setText(message);
        }
        toast.setView(mLytShow);
        toast.setGravity(Gravity.BOTTOM, 0,
                DensityUtils.dp2px(context, 100));
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }
}

/**
 * &#x663E;&#x793A;Toast
 * 
 * @param context
 *            &#x4E0A;&#x4E0B;&#x6587;
 * @param message
 *            &#x4FE1;&#x606F;&#x5185;&#x5BB9;
 */
public static void showIconToast(Context context, String message, int resId) {
    if (isShow) {
        toast = new Toast(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.mtoast_layout, null);
        TextView tv = (TextView) view.findViewById(R.id.toast_tv);
        ImageView mImgShow = (ImageView) view.findViewById(R.id.img_toast);
        if (message != null) {
            tv.setText(message);
        }
        mImgShow.setImageResource(resId);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }
}

/**
 * &#x663E;&#x793A;&#x6D88;&#x606F;&#x5185;&#x5BB9;&#xFF08;&#x5E26;&#x56FE;&#x6807;&#xFF09;
 * 
 * @param context
 *            &#x4E0A;&#x4E0B;&#x6587;
 * @param message
 *            &#x6D88;&#x606F;&#x5185;&#x5BB9;
 * @param resId
 *            &#x56FE;&#x7247;&#x8D44;&#x6E90;id
 * @param textColor
 *            &#x6587;&#x672C;&#x989C;&#x8272;
 * @param textSize
 *            &#x6587;&#x672C;&#x5927;&#x5C0F;
 * @param bgColor
 *            &#x80CC;&#x666F;&#x989C;&#x8272;
 */
public static void showIconToastMessage(Context context, String message,
        int resId, String textColor, float textSize, String bgColor) {
    if (isShow) {
        toast = new Toast(context);
        // &#x56FE;&#x7247;
        mImgIcon = new ImageView(context);
        LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
                DensityUtils.dp2px(context, 48), DensityUtils.dp2px(
                        context, 48));
        mImgIcon.setImageResource(resId);
        lParams.gravity = Gravity.CENTER_HORIZONTAL
                | Gravity.CENTER_VERTICAL;
        lParams.setMargins(5, 5, 5, 5);
        mImgIcon.setLayoutParams(lParams);

        // &#x6D88;&#x606F;&#x5185;&#x5BB9;
        mTxtMsg = new TextView(context);
        LinearLayout.LayoutParams lParams1 = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mTxtMsg.setTextColor(Color.parseColor(textColor));
        mTxtMsg.setTextSize(DensityUtils.dp2px(context, textSize));
        mTxtMsg.setLayoutParams(lParams1);

        mLytShow = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mLytShow.setOrientation(LinearLayout.HORIZONTAL);
        mLytShow.setLayoutParams(params);

        int roundRadius = 100; // 8dp &#x5706;&#x89D2;&#x534A;&#x5F84;
        // int strokeColor = Color.parseColor(&quot;#2E3135&quot;);// &#x8FB9;&#x6846;&#x989C;&#x8272;
        int fillColor = Color.parseColor(bgColor);// &#x5185;&#x90E8;&#x586B;&#x5145;&#x989C;&#x8272;
        GradientDrawable gd = new GradientDrawable();// &#x521B;&#x5EFA;drawable
        gd.setColor(fillColor);
        gd.setCornerRadius(roundRadius);
        // gd.setStroke(0, strokeColor);

        mLytShow.setBackgroundDrawable(gd);
        mLytShow.addView(mImgIcon);
        mLytShow.addView(mTxtMsg);

        if (message != null) {
            mTxtMsg.setText(message);
        }
        toast.setView(mLytShow);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }
}
}</code></pre><p>##&#x518D;&#x6B21;&#xFF0C;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#xFF0C;&#x6211;&#x4E5F;&#x4E00;&#x5E76;&#x8D34;&#x51FA;&#x6765;&#x5427;<br><strong>&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x5C31;&#x4E00;&#x4E2A;ImageView&#x548C;&#x4E00;&#x4E2A;textview</strong><br>&#x200B;<br>&#x200B;      &lt;LinearLayout xmlns:android=&#x201D;<a href="http://schemas.android.com/apk/res/android&quot;" target="_blank" rel="noopener">http://schemas.android.com/apk/res/android&quot;</a><br>&#x200B;    android:layout_width=&#x201D;match_parent&#x201D;<br>&#x200B;    android:layout_height=&#x201D;wrap_content&#x201D;<br>&#x200B;    android:layout_margin=&#x201D;5dp&#x201D;<br>&#x200B;    android:background=&#x201D;@drawable/toast_background&#x201D;<br>&#x200B;    android:orientation=&#x201D;vertical&#x201D; &gt;<br>&#x200B;<br>&#x200B;    &lt;LinearLayout<br>&#x200B;        android:layout_width=&#x201D;wrap_content&#x201D;<br>&#x200B;        android:layout_height=&#x201D;wrap_content&#x201D;<br>&#x200B;        android:layout_gravity=&#x201D;center&#x201D;<br>&#x200B;        android:gravity=&#x201D;center&#x201D;<br>&#x200B;        android:orientation=&#x201D;horizontal&#x201D;<br>&#x200B;        android:padding=&#x201D;5dp&#x201D; &gt;<br>&#x200B;<br>        <imageview android:id="@+id/img_toast" android:layout_width="32dp" android:layout_height="32dp" android:layout_gravity="center" android:src="@drawable/ic_launcher"></imageview></p>
<pre><code>    &lt;TextView
        android:id=&quot;@+id/toast_tv&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;center&quot;
        android:gravity=&quot;center&quot;
        android:text=&quot;moren&quot;
        android:textColor=&quot;#fff&quot;
        android:textStyle=&quot;bold&quot; /&gt;
&lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;</code></pre><p> #&#x6700;&#x540E;</p>
<p><strong>&#x611F;&#x8C22; <a href="http://yongzhi00.iteye.com/blog/1173946/" target="_blank" rel="noopener">qiushyfm</a></strong><br><strong><a href="http://www.jianshu.com/u/6bac141ea5fe" target="_blank" rel="noopener">Jlanglang</a></strong></p>
<p><strong>&#x4E5F;&#x6B22;&#x8FCE;&#x5927;&#x5BB6;&#x5173;&#x6CE8;&#x6211;&#x7684;&#x7B80;&#x4E66;<a href="http://www.jianshu.com/u/1e2eec6f972c" target="_blank" rel="noopener">Silence&#x6F47;&#x6E58;&#x591C;&#x96E8;</a> &#x548C; github<a href="https://github.com/muyishuangfeng" target="_blank" rel="noopener">Silence&#x6F47;&#x6E58;&#x591C;&#x96E8;</a><br>   &#x5C0F;&#x53EF;&#x4E0D;&#x624D;&#xFF0C;&#x5982;&#x679C;&#x4EC0;&#x4E48;&#x5730;&#x65B9;&#x5199;&#x7684;&#x4E0D;&#x5BF9;&#xFF0C;&#x8FD8;&#x8BF7;&#x6307;&#x6B63;&#x3002;</strong></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/08/21/Android观察者模式初探/" rel="next" title="Android观察者模式初探">
                <i class="fa fa-chevron-left"></i> Android观察者模式初探
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/08/21/nexus-3.x私服配置（windows版）/" rel="prev" title="nexus 3.x私服配置（windows版）">
                nexus 3.x私服配置（windows版） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Silence潇湘夜雨</p>
              <p class="site-description motion-element" itemprop="description">Android开发一枚</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/muyishuangfeng" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/1e2eec6f972c" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>简书</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/58df0abf61ff4b006b115e43" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-star"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/yangke20792452" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Silence潇湘夜雨</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">114.8k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

  <!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618},"display":{"superSample":2,"width":150,"height":300,"position":"right","hOffset":0,"vOffset":-20},"mobile":{"show":false},"react":{"opacityDefault":0.5,"opacityOnHover":0.5},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
